#include "stdafx.h"
#include "problem_solver_core.h"
#include "problem.h"
#include "Create Problem Form.h"

namespace SimpleGUI
{
	// Constructor and Destructor

	template <class T, class U> CreateProblemForm<T, U>::CreateProblemForm(nnse::problem<T, U>^ p)
	: parent(p)
	{
		InitializeComponent();
		number_of_training_examples_textBox->Text=parent->get_number_of_training_examples().ToString();
		number_of_testing_examples_textBox->Text=parent->get_number_of_testing_examples().ToString();
		number_of_inputs_textBox->Text=parent->get_number_of_inputs().ToString();
		number_of_targets_textBox->Text=parent->get_number_of_targets().ToString();
		wrap_input_textBox->Text=parent->get_wrap_input().ToString();
		wrap_target_textBox->Text=parent->get_wrap_target().ToString();
	}

	template <class T, class U> CreateProblemForm<T, U>::~CreateProblemForm()
	{
		if (components)
		{
			delete components;
		}
	}

	// GUI Functions

	template <class T, class U> System::Void CreateProblemForm<T, U>::number_of_training_examples_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int number_of_training_examples=int::Parse(number_of_training_examples_textBox->Text);
			if (number_of_training_examples < 1 || number_of_training_examples > 100000)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 100000.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (number_of_training_examples_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Number of Training Examples'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				number_of_training_examples_textBox->Text=parent->get_number_of_training_examples().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::number_of_testing_examples_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int number_of_testing_examples=int::Parse(number_of_testing_examples_textBox->Text);
			if (number_of_testing_examples < 1 || number_of_testing_examples > 100000)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 100000.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (number_of_testing_examples_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Number of Testing Examples'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				number_of_testing_examples_textBox->Text=parent->get_number_of_testing_examples().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::number_of_inputs_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int number_of_inputs=int::Parse(number_of_inputs_textBox->Text);
			if (number_of_inputs < 1 || number_of_inputs > 10000)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 10000.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (number_of_inputs_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Number of Inputs'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				number_of_inputs_textBox->Text=parent->get_number_of_inputs().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::number_of_targets_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int number_of_targets=int::Parse(number_of_targets_textBox->Text);
			if (number_of_targets < 1 || number_of_targets > 10000)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 10000.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (number_of_targets_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Number of Targets'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				number_of_targets_textBox->Text=parent->get_number_of_targets().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::wrap_input_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int wrap_input=int::Parse(wrap_input_textBox->Text);
			if (wrap_input < 1 || wrap_input > 100)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 100.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (wrap_input_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Wrap Input'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				wrap_input_textBox->Text=parent->get_wrap_input().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::wrap_target_textBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			int wrap_target=int::Parse(wrap_target_textBox->Text);
			if (wrap_target < 1 || wrap_target > 100)
			{
				System::FormatException^ e=gcnew System::FormatException("Please enter a number between 1 and 100.");
				throw e;
			}
		}
		catch (System::FormatException^ e)
		{
			if (wrap_target_textBox->Text!="")
			{
				System::Windows::Forms::MessageBox::Show(e->Message, "Error in 'Wrap Target'", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
				wrap_target_textBox->Text=parent->get_wrap_target().ToString();
			}
		}
	};

	template <class T, class U> System::Void CreateProblemForm<T, U>::ok_button_Click(System::Object^  sender, System::EventArgs^  e)
	{
		try
		{
			int number_of_training_examples=int::Parse(number_of_training_examples_textBox->Text);
			int number_of_testing_examples=int::Parse(number_of_testing_examples_textBox->Text);
			int number_of_inputs=int::Parse(number_of_inputs_textBox->Text);
			int number_of_targets=int::Parse(number_of_targets_textBox->Text);
			int wrap_input=int::Parse(wrap_input_textBox->Text);
			int wrap_target=int::Parse(wrap_target_textBox->Text);
			parent->put_number_of_training_examples(static_cast<unsigned int>(number_of_training_examples));
			parent->put_number_of_testing_examples(static_cast<unsigned int>(number_of_testing_examples));
			parent->put_number_of_inputs(static_cast<unsigned int>(number_of_inputs));
			parent->put_number_of_targets(static_cast<unsigned int>(number_of_targets));
			parent->put_wrap_input(static_cast<unsigned int>(wrap_input));
			parent->put_wrap_target(static_cast<unsigned int>(wrap_target));
		}
		catch (System::FormatException^ e)
		{
			System::Windows::Forms::MessageBox::Show(e->Message, "One of the values is invalid", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Asterisk);
		}
	}

	template <class T, class U> System::Void CreateProblemForm<T, U>::cancel_button_Click(System::Object^  sender, System::EventArgs^  e)
	{
	}

	// Explicit Instantiation

	template ref class CreateProblemForm<bool, double>;
	template ref class CreateProblemForm<unsigned char, double>;
}
